Back to course home
0% completed
Vote For New Content
Amazon Hard Problems
Constrained Subsequence Sum (hard)
Solution: Constrained Subsequence Sum
Number of Valid Subarrays (hard)
Solution: Number of Valid Subarrays
Count Vowels Permutation (hard)
Solution: Count Vowels Permutation
Concatenated Words (hard)
Solution: Concatenated Words
Minimum Replacements to Sort the Array (hard)
Solution: Minimum Replacements to Sort the Array
Count All Valid Pickup and Delivery Options (hard)
Solution: Count All Valid Pickup and Delivery Options
Length of the Longest Valid Substring (hard)
Solution: Length of the Longest Valid Substring
Count Array Pairs Divisible by K (hard)
Solution: Count Array Pairs Divisible by K
Word Ladder (hard)
Solution: Word Ladder
Path with Maximum Sum (hard)
Solution: Path with Maximum Sum
Minimum Number of Operations to Make Array Empty (medium)
Problem Statement
You are given an array nums consisting of positive integers.
You can perform below two operations on the array multiple times:
- Select
two elementswithequal valuesanddeletethem from the array. - Select
three elementswithequal valuesanddeletethem from the array.
Return the minimum number of operations required to make the array empty, or -1 if it is not possible.
Examples
Example 1:
- Input: nums =
[1, 3, 2, 1, 2, 2, 3] - Expected Output:
3 - Justification: We can perform the following operations:
- Remove the two
1s (one operation). - Remove the three
2s (second operation). - Remove the two
3s (third operation). Thus, the array can be emptied in 3 operations.
- Remove the two
Example 2:
- Input: nums =
[4, 4, 2, 5, 3, 2, 5, 3] - Expected Output:
4 - Justification: We can perform operations such as removing pairs of
4,2,5, and3in successive steps, each taking one step. This results in the array being emptied in 4 operations.
Example 3:
- Input: nums =
[7, 8, 7] - Expected Output:
-1 - Justification: It is not possible to make the array empty as we can't remove 8 by performing any of 2 operations.
Try it yourself
Try solving this question here:
Java
. . . .
Solution: Minimum Suffix Flips
Solution: Minimum Number of Operations to Make Array Empty
Mark as Completed
On this page
Problem Statement
Examples
Try it yourself